<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 * @property CI_DB_active_record $db
 */

class Post_model extends CI_Model
{

    public function addNew($data = array())
    {
        if (!is_null($data)) {
            return $this->db->insert('post', $data);
        }
        return fasle;
    }
    
    public function getPost($start, $num)
    {
        $return = array();
        $this->db->select('post.*, category.name_vn');
        $this->db->from('post');
        $this->db->join('category', 'category.id = post.parent_id', 'inner');
        $this->db->order_by('modified','desc');
        $this->db->limit($start + $num, $start);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
    public function getNumPost()
    {
        return $this->db->count_all_results('post');
    }
    
    public function updatePost($postId, $array)
    {
        if (!is_null($array)) {
            return $this->db->update('post', $array, array(
                'id'    =>  $postId
            ));
        }
        return false;
    }
    
    public function deletePost($postId)
    {
        $this->db->delete('post', array(
            'id'    =>  $postId
        ));
    }
    
    public function postInfo($postId)
    {
        $return = array();
        $this->db->select('*');
        $this->db->from('post');
        $this->db->where(array(
            'id'    =>  $postId
        ));
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }

}

?>
